﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.03.22
 * 描 述：打印设置
 */
var selectedRow;
var acceptClick;

var bootstrap = function ($, learun) {
    "use strict";
    var companyId = request("companyId");
    var ztId = request("ztId");
    var month = request("month");

    var page = {
        init: function () {
            page.inittree();
            page.bind();
        },
        bind: function () {

            // 打印格式选择
            $('#itemPrintSelect').lrselect({
                type: 'default',
                //placeholder: '请选择帐套',
                // 展开最大高度
                maxHeight: 280,
                allowSearch: true
            });
            // 纸张选择
            $('#paperSelect').lrselect({
                type: 'default',
                data: [{id: "gz_a4_h", value: "gz_a4_h", text: "A4*纵向"},
                    {id: "gz_a4_v", value: "gz_a4_v", text: "A4*横向"},
                    {id: "gz_a3_v", value: "gz_a3_v", text: "A3*横向"}],
                // 展开最大高度
                maxHeight: 180
            });
            $('#itemPrintSelect').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'gzitemprints/dropdownlist?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {companyId: companyId},
                select: function (item) {
                    if (item != null) {
                        $.ajax({
                            url: config.base_server() + 'gzitemprints/findById?id=' + item.id + '&access_token=' + config.getToken().access_token,
                            type: "get",
                            dataType: "json",
                            success: function (result) {
                                var printCol = result.data.columns;
                                setTimeout(function () {
                                    $('#itemTree').lrtreeSet('allNoCheck');
                                    var cols = printCol.split(",");
                                    for (var i = 0; i < cols.length; i++) {
                                        var $div = $('.lr-tree-root').find('div[title=' + cols[i] + ']');
                                        $div.find('.lr-tree-node-cb').trigger('click');
                                    }
                                }, 200);
                            }
                        });

                        var printId = item.id;
                        var reportName = "";
                        if (printId == '01505a2363224a55b2f1e937d766640b') {//事业编
                            reportName = '事编工资表';
                        } else if (printId == '728e8b7af57449eab2357f9410961331') {//事业编短
                            reportName = '住房改革性补贴发放表';
                        } else if (printId == 'a94972c4115845839b490b9efa8675b5') {//行政编
                            reportName = '住房改革性补贴发放表';
                        } else if (printId == '0dfb0894853548b9b9ad7ae3f137f7b5') {//合同工
                            reportName = '合同工工资表';
                        } else {//其他
                            reportName = '工资表';
                        }
                        var title = month.substr(0, 4) + "年" + month.substr(5, 7) + "月" + reportName;
                        $("#title").val(title);
                    }
                }
            });

            // 全选
            $('#select_all').on('click', function (e) {
                $('.lr-tree-node-cb').trigger('click');
            });

            // 全不选
            $('#select_none').on('click', function (e) {
                $('#itemTree').lrtreeSet('allNoCheck');
            });

            // 搜索
            $("#txt_keyword").keydown(function (event) {
                if (event.keyCode == 13) {
                    var keyword = $(this).val();
                    $('#itemTree').lrtreeSet('search', {keyword: keyword});
                }
            });

            var title = month.substr(0, 4) + "年" + month.substr(5, 7) + "月工资表";
            $("#title").val(title);
        },
        inittree: function () {
            if (companyId != "" && ztId != '') {
                $('#itemTree').lrtree({
                    url: config.base_server() + 'gzitems/dropdownlist2?companyId=' + companyId + '&ztId=' + ztId + '&access_token=' + config.getToken().access_token,
                    param: {},
                    nodeDbClick: page.treeNodeDbClick
                });
            }
        },

    };

    // 保存数据
    acceptClick = function (callBack) {
        var postData = {};
        postData.title = $("#title").val();
        postData.columns = $("#itemTree").lrtreeSet('getCheckNodeIds').toString();
        postData.printTemplateCode = $("#paperSelect").lrselectGet();
        postData.hideZero = $('#hideZero').is(':checked') ? 1 : 0;
        postData.ignoreUsername = $("#ignoreUsername").val();

        if (!postData.columns) {
            learun.alert.warning('请选择需要打印的工资款项！');
            return false;
        }

        if (!postData.printTemplateCode) {
            learun.alert.warning('请选择纸张！');
            return false;
        }

        return postData;
    };

    page.init();
}


